package com.mlethe.android.demo.wheelview;

import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;

import androidx.annotation.NonNull;

import com.mlethe.android.demo.R;
import com.mlethe.library.dialog.DialogFragment;
import com.mlethe.library.widget.wheelview.picker.DatePickerView;
import com.mlethe.library.widget.wheelview.picker.DayWheelView;
import com.mlethe.library.widget.wheelview.picker.HourWheelView;
import com.mlethe.library.widget.wheelview.picker.MonthWheelView;
import com.mlethe.library.widget.wheelview.picker.TimeWheelView;
import com.mlethe.library.widget.wheelview.picker.YearWheelView;

/**
 * 时间选择弹窗
 *
 * @author Mlethe
 * @date 2020/9/4
 */
public class DateChooseDialog extends DialogFragment {

    private DatePickerView mDateWv;
    private OnConfirmListener<String> mListener;

    public DateChooseDialog() {
        setNoTitle(true)
                .fullWidth()
                .formBottom(true);
    }

    public DateChooseDialog setOnConfirmListener(OnConfirmListener<String> listener) {
        this.mListener = listener;
        return this;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_date_choose;
    }

    @Override
    protected void onBindViewHolder(Bundle savedInstanceState, ViewHolder holder) {
        setNoTitle(true)
                .fullWidth()
                .formBottom(true);
        holder.setOnClickListener(R.id.closeTv, view -> dismiss())
                .setOnClickListener(R.id.confirmTv, view -> {
                    if (mDateWv != null && mListener != null) {
                        mListener.onClick(mDateWv.getSelectedStr());
                    }
                    dismiss();
                });
        mDateWv = holder.getView(R.id.dateWv);
        mDateWv.setTextSize(17, true);
        mDateWv.setLabelTextSize(16);
        mDateWv.setShowLabel(false);
        mDateWv.setDrawSelectedRect(true);
        mDateWv.setLineSpacing(20, true);
        mDateWv.setTextBoundaryMargin(5, true);
        mDateWv.setSelectedRectColor(Color.parseColor("#F4F4F4"));
        mDateWv.setVisibleItems(5);
        YearWheelView yearWv = mDateWv.getYearWv();
        MonthWheelView monthWv = mDateWv.getMonthWv();
        DayWheelView dayWv = mDateWv.getDayWv();
        HourWheelView hourWv = mDateWv.getHourWv();
        TimeWheelView minuteWv = mDateWv.getMinuteWv();
        TimeWheelView secondWv = mDateWv.getSecondWv();
        yearWv.setIntegerNeedFormat("%d年");
        monthWv.setIntegerNeedFormat("%d月");
        dayWv.setIntegerNeedFormat("%02d日");
        hourWv.setIntegerNeedFormat("%02d时");
        minuteWv.setIntegerNeedFormat("%02d分");
        secondWv.setIntegerNeedFormat("%02d秒");
    }

    @Override
    public void onDismiss(@NonNull DialogInterface dialog) {
        super.onDismiss(dialog);
        mDateWv = null;
        mListener = null;
    }

    public interface OnConfirmListener<T> {
        void onClick(T item);
    }
}
